<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="交易对名称" prop="pairsName">
        <el-input
          v-model="queryParams.pairsName"
          placeholder="交易对名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易对类型">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.trading_pair_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="secondsConfigList">
      <el-table-column label="期货名称" align="center" prop="showPairsName" />
      <el-table-column label="交易对名称" align="center" prop="pairsName" />
      <el-table-column label="涨跌幅" align="center" prop="chPrice" />
      <el-table-column label="涨跌幅百分比" align="center" prop="updown" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.trading_pair_type" :value="scope.row.type"/>
        </template>
      </el-table-column>

      <el-table-column label="最新价" align="center" prop="price" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-value="NORMAL" inactive-value="FROZEN" @change="handleChange(scope.row.id, scope.row.state)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" prop="state">
        <template slot-scope="scope">
          <el-button v-if="scope.row.type == 'CARBON_TRADING'" @click="changeShowPairsName(scope.row)" type="text">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      title="修改名称"
      width="35%"
      append-to-body
    >
    <el-form :rules="rules" :model="changeParams" ref="changeForm" :inline="true" label-width="190px">
      <el-form-item label="交易对名称" prop="showPairsName">
        <el-input
          v-model="changeParams.showPairsName"
          placeholder="交易对名称"
          clearable
          @keyup.enter.native="handleChangePairsName"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleChangePairsName">确认</el-button>
        <el-button @click="resetForm('changeParams');changeParams = {showPairsName:''}">取消</el-button>
      </el-form-item>
    </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listPairs, UpdatePairs, UpdatePairsName } from "@/api/system/jydlb";

export default {
  name: "TradingPairList",
  dicts: ['trading_pair_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      dialogVisible: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pairsName: null,
      },
      changeParams:{showPairsName:''},
      secondsConfigList: [],
      rules: {
        showPairsName: [
          {
            required: true,
            message: '请输入交易对名称',
            trigger: 'blur'
          }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询秒合约配置列表 */
    getList() {
      this.loading = true;
      listPairs(this.queryParams).then(response => {
        this.secondsConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    changeShowPairsName(row){
      this.row = row
      this.changeParams.showPairsName = row.showPairsName
      this.dialogVisible = true
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleChange(id, state) {
      UpdatePairs(id, state).then(response => {
        if (response.code === 200) {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }
      });
    },
    handleChangePairsName() {
      this.$refs.changeForm.validate(valid => {
        if (!valid) return
        UpdatePairsName(this.row.id, {name:this.changeParams.showPairsName}).then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false
            this.getList();
          }
        });
      })
    }
  }
};
</script>
